// Formats LaTeX equations favoring spaces between tokens. // Héctor Calderón // 2010.05.10 // thanks to Robert for suggestions Requires(20100317); StartWorking; BeginGroup; PushTagsandRegisters; IfSel( "1", "<>", "LetRegNum(5,1);", "LetRegNum(5,0);" ); IfSel( "1", "<>", "MatchDelimiter( 1, 0, 0, 1 );", "Relax;" ); //If it could not find matching delimiters, then return with error message IfOK( *Relax;*, *Prompt('There was an error matching the delimiters', 3, 1 ); JMP('TheExit');* ); LetReg( 0, "%S" ); // Replace indentations with chr(127) SubstituteInString( '%!0', '>\(+[ ]\)', '>\s{ }{%&127;}{\0}', 1, 0 ); // Space before \ but not between \\ SubstituteInString( '%!0', '\#~{\\}\\', ' \\', 1, 0 ); // Space before & but not between && SubstituteInString( '%!0', '\#~{\&}\&', ' \&', 1, 0 ); // one new line after and one space before \\ SubstituteInString( '%!0', '@[%\ ]\\\\@[%\%%]', ' \\\\%\', 1, 0 ); // only one spaces before \\ SubstituteInString( '%!0', '@[ ]\\\\', ' \\\\', 1, 0 ); // one space before { and no spaces after { SubstituteInString( '%!0', '\{@[ ]', ' \{', 1, 0 ); // no spaces before } and one space after } SubstituteInString( '%!0', '@[ ]\}', '\} ', 1, 0 ); // one space after ( SubstituteInString( '%!0', '(', '( ', 0, 0 ); // one space after , SubstituteInString( '%!0', ',', ', ', 0, 0 ); // one space before and after ) SubstituteInString( '%!0', ')', ' ) ', 0, 0 ); // one space before +-*/><=& SubstituteInString( '%!0', '\([\+\-\*\/\>\<\=]\)', ' \0 ', 1, 0 ); // no spaces between +- and alphanumeric at the beginning of an expression SubstituteInString( '%!0', '\(1[\=\{\>\<]\)+[ ]\([\+\-]\)+[ ]\(2$Alpha+Numeric$\)', '\1 \0\2', 1, 0 ); // no spaces around ^ or _ SubstituteInString( '%!0', '@[ ]\([\^\_]\)@[ ]', '\0', 1, 0 ); // no spaces between eft and (, \, or [ SubstituteInString( '%!0', 'eft+[ ]\({(|\\|\[}\)', 'eft\0', 1, 0 ); // no spaces between ight and ), \, or ] SubstituteInString( '%!0', 'ight+[ ]\({)|\\|\]}\)', 'ight\0', 1, 0 ); // no more than one space in a row SubstituteInString( '%!0', ' +[ ]', ' ', 1, 0 ); // no spaces if the parenthesis enclose a single character SubstituteInString( '%!0', '( \(.\) )', '(\0)', 1, 0 ); // no spaces at the beginning or end FormatString( '%!0', 0, "trimboth" ); // no spaces around $ SubstituteInString( "%!0", "@[ ]\$@[ ]", "\$", 1, 0); // no spaces between either of begin, end, or label and { SubstituteInString( "%!0", "\\\({{label}|{begin}|{end}}\) \{", "\\\0\{", 1, 0); // no spaces for the arguments of label, begin, end SubstituteInString( "%!0", "\\\({{label}|{begin}|{end}}\)\{\(1*\)\}", "\\\0\{\s{ }{}{\1}\}", 1, 0); // no spaces between & and = SubstituteInString( '%!0', '& =', '&=', 0, 0 ); // restore indentation SubstituteInString( "%!0", "%&127;+[ ]", "%&127;", 1, 0); SubstituteInString( "%!0", ">+[ ]", ">", 1, 0); SubstituteInString( "%!0", "%&127;", " ", 1, 0); // no blank lines SubstituteInString( "%!0", ">+[ ]<", "><", 1, 0); SubstituteInString( "%!0", "%\>", ">", 1, 0); //Prompt( "%!0" ); //JMP( "TheExit" ); Delete; Mark( 9 ); Ins( "%!0" ); Mark( 8 ); RestoreSelection( 8, 9, 0 ); :TheExit:: PopTagsandRegisters; EndGroup; StopWorking;